package com.selfdriving.activity;

import java.util.ArrayList;
import java.util.List;
import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import com.selfdriving.R;
import com.selfdriving.bean.Preference;

public class ChangeSkinActivity extends BaseActivity {
	@ViewInject(id = R.id.topBack, click = "topBack")
	ImageView topBack;

	@ViewInject(id = R.id.topText)
	TextView topText;

	@ViewInject(id = R.id.topRight, click = "topRight")
	Button topRight;

	@ViewInject(id = R.id.skinView)
	GridView skinView;

	public List<Skin> skins;

	private HolderView holderView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.change_skin);
		final PictureAdapter adapter = new PictureAdapter();
		skins = new ArrayList<Skin>();
		for (int i = 0; i < 5; i++) {
			Skin skin = new Skin();
			skin.isSet = false;
			skin.backDraw = R.drawable.bg_skin_1;
			skins.add(skin);
		}
		topText.setText("设置背景");
		topRight.setVisibility(View.GONE);
		skinView.setAdapter(adapter);
		skinView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				for (int i = 0; i < skins.size(); i++) {
					if (i == arg2) {
						skins.get(i).isSet = true;
						putInt(Preference.PERSON_BACKGROUND, skins.get(i).backDraw, true);
					}else{
						skins.get(i).isSet = false;
					}
				}
				adapter.notifyDataSetChanged();
			}
		});
	}
	
	public void topBack(View v){
		this.finish();
	}

	public class PictureAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return skins.size();
		}

		@Override
		public Object getItem(int arg0) {
			return skins.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int poistion, View view, ViewGroup arg2) {
			Skin skin = skins.get(poistion);
			if (view == null) {
				holderView = new HolderView();
				view = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE))
						.inflate(R.layout.skin_item, null);
				view.setTag(holderView);
			} else {
				holderView = (HolderView) view.getTag();
			}
			holderView.ivSkin = (ImageView) view.findViewById(R.id.ivSkin);
			holderView.ivSet = (ImageView) view.findViewById(R.id.ivSet);
			if (skin.isSet) {
				holderView.ivSet.setVisibility(View.VISIBLE);
			} else {
				holderView.ivSet.setVisibility(View.GONE);
			}
			return view;
		}

	}

	public class Skin {
		public boolean isSet;
		
		public int backDraw;
	}

	public class HolderView {
		public ImageView ivSkin;

		public ImageView ivSet;
	}

}
